package com.wzq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzq.controller.admin.GetSome;
import com.wzq.dao.pojo.*;
import com.wzq.service.IMytrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/mytrack")
public class MytrackController extends GetSome {

    @Autowired
    private IMytrackService iMytrackService;
    //   添加
    @PostMapping
    public Result save(@RequestBody Mytrack mytrack) {
        List<Mytrack> list=new ArrayList<>();
        boolean flag = iMytrackService.save(mytrack);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
    //   添加
    @PostMapping("/save")
    public Result saves(@RequestBody Mytrack mytrack) {
        try {
            LambdaQueryWrapper<Mytrack> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Mytrack::getW_id,mytrack.getW_id()).eq(Mytrack::getP_id,mytrack.getP_id());
//            Mytrack one = iMytrackService.getOne(lqw);
            boolean flag;
            if  ( iMytrackService.getOne(lqw)==null){
                mytrack.setTitle( getP_name(mytrack.getP_id()) );
                System.out.println(1);
//                mytrack.setW_name( getW_name(mytrack.getW_id()) );
                System.out.println(2);
                flag=iMytrackService.save(mytrack);
                return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,true);
            }
            else return new Result(Code.SAVE_ERR,true);
        } catch ( Exception e) {
            return new Result(Code.SAVE_ERR,true);
        }
    }
    //   修改
    @PutMapping
    public Result update(@RequestBody Mytrack mytrack) {
        boolean flag = iMytrackService.updateById(mytrack);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    //     获得指定须知
    @GetMapping("/track/{w_id}")
    public Result getByTitle(@PathVariable Integer w_id) {
        LambdaQueryWrapper<Mytrack> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Mytrack::getW_id , w_id);
        List<Mytrack> mytrack = iMytrackService.list(lqw);
        Integer code = mytrack != null ? Code.GET_OK : Code.GET_ERR;
        String msg = mytrack != null ? "" : "数据查询失败，请重试！";
        return new Result(code,mytrack,msg);
    }

    //    id删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag = iMytrackService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    //     获得所有用户信息
    @GetMapping
    public Result getAll() {
        List<Mytrack> list = iMytrackService.list();
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

}
